/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Discribe：摄像机跟随鼠标移动晃动镜头
* CreateTime：2020-05-14 11:33:18
* Version：1.0
* Modify Recoder：
*************************************************/

using UnityEngine;

namespace TinyToolKit
{
    public class CameraBob : MonoBehaviour
    {
        public Camera mainCam;
        public float minXAngle = -3;
        public float maxXAngle = 3;
        public float minYAngle = -3;
        public float maxYAngle = 3; 
        public float xSensitivity = 1f;
        public float ySensitivity = 1f;
        public float smooth = 1;
    
        private Vector3 targetEulerAngle;
        private Vector3 eulerAngle;
        private Quaternion originalRotation;
    
        private void Start()
        {
            if(mainCam == null)
                mainCam = Camera.main;

            if (mainCam != null) 
                originalRotation = mainCam.transform.rotation;
        }

        private void Update()
        {
            float yRot = Input.GetAxis("Mouse X") * xSensitivity;
            float xRot = Input.GetAxis("Mouse Y") * ySensitivity;
        
            targetEulerAngle.x -= xRot;
            targetEulerAngle.y += yRot;

            targetEulerAngle.x = Mathf.Clamp(targetEulerAngle.x, minXAngle, maxXAngle);
            targetEulerAngle.y = Mathf.Clamp(targetEulerAngle.y, minYAngle, maxYAngle);
        
            eulerAngle = Vector3.Lerp(eulerAngle, targetEulerAngle, Time.deltaTime * smooth);
            mainCam.transform.rotation = originalRotation * Quaternion.Euler(eulerAngle);
        }
    }
}
